package com.changxianggu.cloud.test;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.changxianggu.cloud.edu.tool.entity.vo.CourseVO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 处理班级拆分 计算机科学与技术2017-0[1-7]班
 * 拆分成计算机科学与技术2017-01，计算机科学与技术2017-02...计算机科学与技术2017-07等
 */
@Slf4j
public class SplitSeriesClazzName {

    public static final String INVALID_STR2 = "]";
    public static final String INVALID_STR3 = "\\[";

    public static final String CLAZZ_SERIES_STR = "-";

    @Test
    public void testSplitSeries() {
        String filePath = "D:\\课程表0605.xls";
        List<Map<String, Object>> listMap = new ArrayList<>();
        File f = new File(filePath);
        ExcelReader reader = ExcelUtil.getReader(f);
        List<CourseVO> courseVOList = reader.readAll(CourseVO.class);
        for (CourseVO courseVO : courseVOList) {
            String clazzName = courseVO.getClazzName();
            log.debug(clazzName);
            String resultClazzName = handlerClazzName(clazzName);
            courseVO.setClazzName(resultClazzName);
        }
        write2Local(courseVOList);
    }

    @Test
    public void testStr() {
        String ss = "林产化工2019-0[1-2]班,园林2019-0[1-3]班,木材科学与工程（卓越工程师）2019-01班,野生动物与自然保护区管理2019-0[1-2]班";
        String handlerClazzName = handlerClazzName(ss);
        log.debug(handlerClazzName);
    }

    public String handlerClazzName(String clazzName) {
        String result = "";
        if (StrUtil.isBlank(clazzName)) {
            return result;
        }
        clazzName = clazzName.replaceAll("，", ",");
        String[] clazzList = clazzName.split(",");
        List<String> clazzNameList = new ArrayList<>();
        for (String s : clazzList) {
            String cName = s;
            cName = cName.replaceAll(INVALID_STR3, "").replaceAll(INVALID_STR2, "");
            if (cName.endsWith("班")) {
                cName = cName.substring(0, cName.length() - 1);
            }
            if (StrUtil.count(cName, CLAZZ_SERIES_STR) == 2) {
                String[] target = cName.split(CLAZZ_SERIES_STR);
                String clazzNameHeader = target[0];
                String clazzNameStart = target[1];
                String clazzNameEnd = target[2];
                int start = Integer.parseInt(clazzNameStart);
                int end = Integer.parseInt(clazzNameEnd);
                if (start >= 10) {
                    end = 10 + end;
                }
                for (int j = start; j <= end; j++) {
                    String tmpClazzName = clazzNameHeader + (j < 10 ? "-0" + j : "-" + j);
                    clazzNameList.add(tmpClazzName);
                }
            } else {
                clazzNameList.add(cName);
            }
        }
        result = StrUtil.join(",", clazzNameList);
        return result;
    }

    public void write2Local(List<CourseVO> dataList) {
        String fullPath = "D:\\拆分结果.xlsx";
        OutputStream out = null;

        try {
            //创建临时文件
            File tmpFile = new File(fullPath);
            out = new FileOutputStream(tmpFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExcelWriter writer = EasyExcelFactory.getWriter(out, ExcelTypeEnum.XLSX, true);
        //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
        Sheet sheet = new Sheet(1, 1, CourseVO.class);
        //or 设置自适应宽度
        sheet.setAutoWidth(Boolean.TRUE);
        writer.write(dataList, sheet);
        writer.finish();
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
